import { BOSS, boss, WORKER, worker } from "../../store/reducers/UserRole.ts";
import RNMMKV from "../../../Native/Module/RNMMKV.ts";
import javaFetch from "../../../Core/Net/JavaNet.ts";
import store from "../../store/store.ts";

class RoleLds {

  saveRole(role: number) {
    RNMMKV.saveString("role", `${role}`);
    if (role == BOSS) {
      store.dispatch(boss());
    } else {
      store.dispatch(worker());
    }
  }

  getRole(): number {
    const role = RNMMKV.getString("role") || `${WORKER}`;
    return parseInt(role);
  }
}


class RoleRds {

  syncRole(role: number) {
    javaFetch({ url: "account/v1/role/changeRole", params: { "role": role } }).then(value => {
      console.log("sync role success");
    });
  }
}


export class RoleRep {

  private readonly roleLds = new RoleLds();

  private readonly roleRds = new RoleRds();

  selectRole(role: number) {
    if (this.getRole() == role) return;
    this.roleLds.saveRole(role);
    this.roleRds.syncRole(role);
  }

  getRole(): number {
    return this.roleLds.getRole();
  }
}
